package ph.com.gs3.formalistics.presenter.fragements.workers;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.model.ldao.DocumentsLDAO;
import ph.com.gs3.formalistics.model.ldao.FormsLDAO;
import ph.com.gs3.formalistics.model.vo.application.SearchCondition;
import ph.com.gs3.formalistics.model.vo.business.User;
import ph.com.gs3.formalistics.model.vo.business.form.Form;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData.InvalidFormFieldException;
import ph.com.gs3.formalistics.model.vo.formcontent.PickListData;
import android.app.Fragment;
import android.content.Context;

public class PickListPickerWorkerFragment extends Fragment {

	public static final String TAG = PickListPickerWorkerFragment.class.getSimpleName();

	private FormsLDAO formsLDAO;
	private DocumentsLDAO documentsLDAO;

	private User currentUser;
	private String fieldId;
	private PickListData searchAndResultData;

	public static PickListPickerWorkerFragment createInstance(Context context) {

		PickListPickerWorkerFragment instance = new PickListPickerWorkerFragment();
		instance.formsLDAO = new FormsLDAO(context);
		instance.documentsLDAO = new DocumentsLDAO(context);
		return instance;

	}

	public List<JSONObject> getPicklistData(String filter) {

		String formWebId = searchAndResultData.getFormWebId();

		List<String> displayFieldIds = searchAndResultData.getViewColumnIdList();
		List<String> dataFieldIds = new ArrayList<>();

		dataFieldIds.addAll(searchAndResultData.getViewColumnIdList());
		dataFieldIds.add(searchAndResultData.getResultFieldName());

		Form form = null;

		try {
			formsLDAO.open();
			form = formsLDAO.getForm(formWebId, currentUser.getCompany().getId());
		} catch (JSONException | InvalidFormFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			formsLDAO.close();
		}

		// Create conditions from the filter
		List<SearchCondition> conditions = null;

		if (filter != null) {
			conditions = createConditionsFromFilter(filter, displayFieldIds);
		}

		List<JSONObject> data = new ArrayList<>();

		try {
			documentsLDAO.open();
			data = documentsLDAO.search(form, dataFieldIds, currentUser.getId(), conditions);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			documentsLDAO.close();
		}

		return data;

	}

	private List<SearchCondition> createConditionsFromFilter(String filter, List<String> fieldIds) {

		List<SearchCondition> conditions = new ArrayList<>();

		for (String fieldId : fieldIds) {
			conditions.add(new SearchCondition(fieldId, " LiKE ", filter + "%"));
		}

		return conditions;

	}

	// ========================================================================
	// {{ Getters & Setters

	public User getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	public String getFieldId() {
		return fieldId;
	}

	public void setFieldId(String fieldId) {
		this.fieldId = fieldId;
	}

	public PickListData getSearchAndResultData() {
		return searchAndResultData;
	}

	public void setSearchAndResultData(PickListData searchAndResultData) {
		this.searchAndResultData = searchAndResultData;
	}

	// }}

}
